package com.hontech.bvapp2.ui.parts.cargo

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.Button
import android.widget.FrameLayout
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.cp.Cargo
import com.hontech.bvapp2.ui.dialog.runProg
import com.hontech.bvapp2.ui.view.LabelInput
import com.hontech.bvapp2.ui.view.LabelSpinner
import com.hontech.bvapp2.utils.onClick

class CargoCtrlParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_cargo_cargo_ctrl, this)
        val attr = context.obtainStyledAttributes(attrs, R.styleable.CargoCtrlParts)
        val cargo = attr.getInteger(R.styleable.CargoCtrlParts_cargo_cargo, 0)
        attr.recycle()

        val ctrl = view.findViewById<Button>(R.id.id_cargo_ctrl_btn)
        val li = view.findViewById<LabelInput>(R.id.id_cargo_li_position)
        val scanCtrl = view.findViewById<Button>(R.id.id_cargo_scan_btn)
        val deliveryCtrl = view.findViewById<Button>(R.id.id_cargo_delivery_btn)
        val sp = view.findViewById<LabelSpinner>(R.id.id_cargo_sp_row)

        ctrl.onClick {
            runProg("货架电机", "控制") {
                val position = li.textInt()
                Cargo.cargoCtrl(cargo, position)
            }
        }

        scanCtrl.onClick {
            runProg("货架电机", "扫码位置") {
                val position = Cargo.cargoScanPositionWithRow( sp.selectIndex() + 1 )
                Cargo.cargoCtrl(cargo, position)
            }
        }

        deliveryCtrl.onClick {
            runProg("货架电机", "出货位置") {
                val position = Cargo.cargoPickPositionWithRow( sp.selectIndex() + 1 )
                Cargo.cargoCtrl(cargo, position)
            }
        }
    }
}